<?php
/**
 * Created by PhpStorm.
 * User: zyd
 * Date: 2019/2/28
 * Time: 21:26
 */

namespace app\shawting\controller;
use think\Db;
use think\Validate;

class Classify extends Base
{
    public function index(){   
        $arr = db("city_classify")
            ->where(["classify_type"=>1,"delete_flag"=>1])
            ->order("order_by,parent_id,order_son,id")
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $this->assign("arr",$arr);
        return $this->fetch();
    }

    public function ajax_index_edit(){
        $id = input('id');
        $classify_name = input('classify_name');
        if(!is_numeric($id) || empty($classify_name)){
            return $this->error("提交内容不能为空！");
        }
        $re = db("city_classify")->where(['id'=>$id])->update(['classify_name'=>$classify_name]);
        if($re){
            return $this->success("修改成功");
        }else{
            return $this->error("修改失败");
        }
    }

    public function ajax_index_first()
    {
        $classify_name = input("classify_name");
        if(empty($classify_name)){
            return $this->error("分类名称不能为空");
        }
        $data = [
            "classify_name"=>$classify_name,
            "show_flag"=>1,
            "delete_flag"=>1,
            "classify_type"=>1,
            "parent_id"=>0
        ];
        $id = db("city_classify")->insertGetId($data);
        if(!$id){
            return $this->error("添加失败");
        }
        $arr=[
            "classify_no"=>$id,
            "order_by"=>$id,
            "order_son"=>$id,
        ];
        $re = db("city_classify")->where(["id"=>$id])->update($arr);
        if($re){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }
    }

    public function ajax_index_two()
    {
        $classify_name = input("classify_name");
        $parent_id = input("parent_id");
        if(!is_numeric($parent_id) || empty($classify_name)){
            return $this->error("分类名称不能为空");
        }
        $data = [
            "classify_name"=>$classify_name,
            "show_flag"=>1,
            "delete_flag"=>1,
            "classify_type"=>1,
            "parent_id"=>$parent_id,
            "order_by"=>$parent_id,
        ];
        $id = db("city_classify")->insertGetId($data);
        if(!$id){
            return $this->error("添加失败");
        }
        $arr=[
            "classify_no"=>$id,
            "order_son"=>$id,
        ];
        $re = db("city_classify")->where(["id"=>$id])->update($arr);
        if($re){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }
    }

    public function ajax_index_delete(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error('查询数据格式不正确');
        }
        $count = db("city_classify")->where(['parent_id'=>$id])->count();
        if($count){
            return $this->error("存在子级无法删除");
        }
        $re = db("city_classify")->where(["id"=>$id])->update(["delete_flag"=>0]);
        if(!$re){
            return $this->error("删除失败");
        }else{
            return $this->success("删除成功");
        }
    }

    public function ajax_show_flag(){
        $id = input("id");
        $show_flag = input("show_flag");
        if(!is_numeric($id) && !is_numeric($show_flag)){
            return $this->error('查询数据格式不正确');
        }
        $data = db("city_classify")->where(["id"=>$id])->whereOr(["parent_id"=>$id])->update(["show_flag"=>$show_flag]);
        if(!$data){
            $this->error("操作失败！");
        }
        $this->success("操作成功",null,["num"=>$data]);
    }

    public function ajax_sort(){
        //type:0向下,type:1向上
        $type = input("type");
        $order_by = input("order_by");
        $parent_id = input("parent_id");
        $order_son = input("order_son");
        if($parent_id == 0){
            $orderBy = "order_by";
            if($type == 0){
                $where[$orderBy] = [">",$order_by];
                $order = "order_by,parent_id,order_son,id";
            }else{
                $where[$orderBy] = ["<",$order_by];
                $order = "order_by desc,parent_id,order_son,id";
            }
        }else{
            $orderBy = "order_son";
            if($type == 0){
                $where[$orderBy] = [">",$order_son];
                $order = "order_by,parent_id,order_son,id";
            }else{
                $where[$orderBy] = ["<",$order_son];
                $order = "order_by,parent_id,order_son desc,id";
            }
        }
        $where['parent_id'] = $parent_id;
        $where['classify_type'] =1;
        $where['delete_flag'] =1;
        $data = db("city_classify")->where($where)->order($order)->find();
        if(!$data){
            return $this->error('已经到极限了');
        }
        if($parent_id == 0){
            $re = Db::execute("update t_city_classify A join t_city_classify B on (A.order_by = {$order_by} and B.order_by = {$data['order_by']}) SET A.order_by = B.order_by,B.order_by = A.order_by where A.classify_type = 1 and A.delete_flag =1 and B.classify_type = 1 and B.delete_flag =1");
        }else{
            $re = Db::execute("update t_city_classify A join t_city_classify B on (A.order_son={$order_son} and B.order_son={$data['order_son']}) SET A.order_son = B.order_son,B.order_son = A.order_son where A.classify_type = 1 and A.delete_flag =1 and B.classify_type = 1 and B.delete_flag =1 and A.parent_id = {$parent_id} and B.parent_id = {$parent_id}");
        }
        if($re){
            return $this->success("操作成功");
        }else{
            return $this->error("操作失败");
        }
    }

    public function ajax_directory_sort(){
        //type:0向下,type:1向上
        $type = input("type");
        $order_by = input("order_by");
        $orderBy = "order_by";
        if($type == 0){
            $where[$orderBy] = [">",$order_by];
            $order = "order_by,parent_id,order_son,id";
        }else{
            $where[$orderBy] = ["<",$order_by];
            $order = "order_by desc,parent_id,order_son,id";
        }

        $where['parent_id'] = 0;
        $where['classify_type'] =2;
        $where['delete_flag'] =1;
        $data = db("city_classify")->where($where)->order($order)->find();
        if(!$data){
            return $this->error('已经到极限了');
        }
        $re = Db::execute("update t_city_classify A join t_city_classify B on (A.order_by = {$order_by} and B.order_by = {$data['order_by']}) SET A.order_by = B.order_by,B.order_by = A.order_by where A.classify_type = 2 and A.delete_flag =1 and B.classify_type = 2 and B.delete_flag =1");
        if($re){
            return $this->success("操作成功");
        }else{
            return $this->error("操作失败");
        }
    }

    public function directory(){
        $arr = db("city_classify")
            ->where(["classify_type"=>2,"delete_flag"=>1])
            ->order("order_by")
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $this->assign("arr",$arr);
        return $this->fetch();
    }

    public function ajax_directory_first(){
        $classify_name = input("classify_name");
        if(empty($classify_name)){
            return $this->error("分类名称不能为空");
        }
        $data = [
            "classify_name"=>$classify_name,
            "show_flag"=>1,
            "delete_flag"=>1,
            "classify_type"=>2,
            "parent_id"=>0
        ];
        $id = db("city_classify")->insertGetId($data);
        if(!$id){
            return $this->error("添加失败");
        }
        $arr=[
            "classify_no"=>$id,
            "order_by"=>$id,
            "order_son"=>$id,
        ];
        $re = db("city_classify")->where(["id"=>$id])->update($arr);
        if($re){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }
    }

    public function details(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error('查询数据格式不正确');
        }
        $arr = db("city_directory")->alias("A")
            ->join("t_city_directory_detail B","A.detail_id = B.id")
            ->field("A.*,directory_detail1,directory_detail2,directory_detail3,directory_detail4,directory_detail5,directory_detail6")
            ->where(["classify_id"=>$id,"delete_flag"=>1])
            ->paginate(10,false, ['query'=> request()->param()]/*不丢失已存在的url参数]*/);
        $this->assign("arr",$arr);
        return $this->fetch();
    }

    public function ajax_details_add(){
        $arr = input();
        $directory_data = [
            "classify_id" => $arr['classify_id'],
            "classify_name" => $arr["classify_name"],
            "org_name" => $arr["org_name"],
            "address" => $arr['address']
        ];
        $details_data = [
            "directory_detail1" => $arr["directory_detail1"],
            "directory_detail2" => $arr["directory_detail2"],
            "directory_detail3" => $arr["directory_detail3"],
            "directory_detail4" => $arr["directory_detail4"],
            "directory_detail5" => $arr["directory_detail5"],
            "directory_detail6" => $arr["directory_detail6"],
        ];
        $detail_id = db("city_directory_detail")->insertGetId($details_data);
        if(!$detail_id){
            return $this->error("添加联系方式失败");
        }
        $directory_data["detail_id"] = $detail_id;
        $directory_data["delete_flag"] = 1;
        $validate = new Validate([
            "org_name" => "require",
            "address" => "require",

        ],
            [
                "org_name" => "机构名称不能为空",
                "address" => "联系地址不能为空",
            ]
        );
        if(!$validate->check($directory_data)){
            $this->error($validate->getError());
        }
        $directory_result = db("city_directory")->insert($directory_data);
        if($directory_result){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }
    }

    public function ajax_directory_delete(){
        $id = input("id");
        if(!is_numeric($id)){
            return $this->error('查询数据格式不正确');
        }
        $re = db("city_directory")->where(["id"=>$id])->update(["delete_flag"=>0]);

        if($re){
            return $this->success("操作成功");
        }else{
            return $this->error("操作失败");
        }
    }





































}